/*     */ package com.openbravo.pos.forms;
/*     */ 
/*     */ import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.openbravo.basic.BasicException;
/*     */ import com.openbravo.data.loader.DataRead;
/*     */ import com.openbravo.data.loader.Datas;
/*     */ import com.openbravo.data.loader.ImageUtils;
/*     */ import com.openbravo.data.loader.PreparedSentence;
/*     */ import com.openbravo.data.loader.SentenceExec;
/*     */ import com.openbravo.data.loader.SentenceFind;
/*     */ import com.openbravo.data.loader.SentenceList;
/*     */ import com.openbravo.data.loader.SerializerRead;
/*     */ import com.openbravo.data.loader.SerializerReadBasic;
/*     */ import com.openbravo.data.loader.SerializerReadBytes;
/*     */ import com.openbravo.data.loader.SerializerReadInteger;
/*     */ import com.openbravo.data.loader.SerializerReadString;
/*     */ import com.openbravo.data.loader.SerializerWriteBasic;
/*     */ import com.openbravo.data.loader.SerializerWriteBasicExt;
/*     */ import com.openbravo.data.loader.SerializerWriteString;
/*     */ import com.openbravo.data.loader.Session;
/*     */ import com.openbravo.data.loader.SessionDB;
/*     */ import com.openbravo.data.loader.StaticSentence;
/*     */ import com.openbravo.format.Formats;
/*     */ import com.openbravo.pos.util.ThumbNailBuilder;

/*     */ import java.awt.image.BufferedImage;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Properties;
/*     */ import java.util.UUID;

/*     */ import javax.imageio.ImageIO;
/*     */ import javax.swing.ImageIcon;
/*     */ 
/*     */ public class DataLogicSystem extends BeanFactoryDataSingle
/*     */ {
/*     */   protected String m_sInitScript;
/*     */   private SentenceFind m_version;
/*     */   private SentenceExec m_dummy;
/*     */   protected SentenceList m_peoplevisible;
/*     */   protected SentenceFind m_peoplebycard;
/*     */   protected SerializerRead peopleread;
/*     */   private SentenceFind m_rolepermissions;
/*     */   private SentenceExec m_changepassword;
/*     */   private SentenceFind m_locationfind;
/*     */   private SentenceFind m_resourcebytes;
/*     */   private SentenceExec m_resourcebytesinsert;
/*     */   private SentenceExec m_resourcebytesupdate;
/*     */   protected SentenceFind m_sequencecash;
/*     */   protected SentenceFind m_activecash;
/*     */   protected SentenceExec m_insertcash;
/*     */   private Map<String, byte[]> resourcescache_Map;
/*     */ 
/*     */   public void init(Session s)
/*     */   {
/*  70 */     this.m_sInitScript = ("/com/openbravo/pos/scripts/" + s.DB.getName());
/*     */ 
/*  72 */     this.m_version = new PreparedSentence(s, "SELECT VERSION FROM APPLICATIONS WHERE ID = ?", SerializerWriteString.INSTANCE, SerializerReadString.INSTANCE);
/*  73 */     this.m_dummy = new StaticSentence(s, "SELECT * FROM PEOPLE WHERE 1 = 0");
/*     */ 
/*  75 */     final ThumbNailBuilder tnb = new ThumbNailBuilder(32, 32, "com/openbravo/images/yast_sysadmin.png");
/*  76 */     this.peopleread = new SerializerRead() {
/*     */       public Object readValues(DataRead dr) throws BasicException {
/*  78 */         return new AppUser(dr.getString(1), dr.getString(2), dr.getString(3), dr.getString(4), dr.getString(5), new ImageIcon(tnb.getThumbNail(ImageUtils.readImage(dr.getBytes(6)))));
/*     */       }
/*     */     };
/*  88 */     this.m_peoplevisible = new StaticSentence(s, "SELECT ID, NAME, APPPASSWORD, CARD, ROLE, IMAGE FROM PEOPLE WHERE VISIBLE = " + s.DB.TRUE(), null, this.peopleread);
/*     */ 
/*  93 */     this.m_peoplebycard = new PreparedSentence(s, "SELECT ID, NAME, APPPASSWORD, CARD, ROLE, IMAGE FROM PEOPLE WHERE CARD = ? AND VISIBLE = " + s.DB.TRUE(), SerializerWriteString.INSTANCE, this.peopleread);
/*     */ 
/*  98 */     this.m_resourcebytes = new PreparedSentence(s, "SELECT CONTENT FROM RESOURCES WHERE NAME = ?", SerializerWriteString.INSTANCE, SerializerReadBytes.INSTANCE);
/*     */ 
/* 103 */     Datas[] resourcedata = { Datas.STRING, Datas.STRING, Datas.INT, Datas.BYTES };
/* 104 */     this.m_resourcebytesinsert = new PreparedSentence(s, "INSERT INTO RESOURCES(ID, NAME, RESTYPE, CONTENT) VALUES (?, ?, ?, ?)", new SerializerWriteBasic(resourcedata));
/*     */ 
/* 107 */     this.m_resourcebytesupdate = new PreparedSentence(s, "UPDATE RESOURCES SET NAME = ?, RESTYPE = ?, CONTENT = ? WHERE NAME = ?", new SerializerWriteBasicExt(resourcedata, new int[] { 1, 2, 3, 1 }));
/*     */ 
/* 111 */     this.m_rolepermissions = new PreparedSentence(s, "SELECT PERMISSIONS FROM ROLES WHERE ID = ?", SerializerWriteString.INSTANCE, SerializerReadBytes.INSTANCE);
/*     */ 
/* 116 */     this.m_changepassword = new StaticSentence(s, "UPDATE PEOPLE SET APPPASSWORD = ? WHERE ID = ?", new SerializerWriteBasic(new Datas[] { Datas.STRING, Datas.STRING }));
/*     */ 
/* 120 */     this.m_sequencecash = new StaticSentence(s, "SELECT MAX(HOSTSEQUENCE) FROM CLOSEDCASH WHERE HOST = ?", SerializerWriteString.INSTANCE, SerializerReadInteger.INSTANCE);
/*     */ 
/* 124 */     this.m_activecash = new StaticSentence(s, "SELECT HOST, HOSTSEQUENCE, DATESTART, DATEEND FROM CLOSEDCASH WHERE MONEY = ?", SerializerWriteString.INSTANCE, new SerializerReadBasic(new Datas[] { Datas.STRING, Datas.INT, Datas.TIMESTAMP, Datas.TIMESTAMP }));
/*     */ 
/* 128 */     this.m_insertcash = new StaticSentence(s, "INSERT INTO CLOSEDCASH(MONEY, HOST, HOSTSEQUENCE, DATESTART, DATEEND) VALUES (?, ?, ?, ?, ?)", new SerializerWriteBasic(new Datas[] { Datas.STRING, Datas.STRING, Datas.INT, Datas.TIMESTAMP, Datas.TIMESTAMP }));
/*     */ 
/* 133 */     this.m_locationfind = new StaticSentence(s, "SELECT NAME FROM LOCATIONS WHERE ID = ?", SerializerWriteString.INSTANCE, SerializerReadString.INSTANCE);
/*     */ 
/* 138 */     resetResourcesCache();
/*     */   }
/*     */ 
/*     */   public String getInitScript()
/*     */   {
/* 143 */     return this.m_sInitScript;
/*     */   }
/*     */ 
/*     */   public final String findVersion()
/*     */     throws BasicException
/*     */   {
/* 149 */     return (String)this.m_version.find("powerfulpos");
/*     */   }
/*     */   public final void execDummy() throws BasicException {
/* 152 */     this.m_dummy.exec();
/*     */   }
/*     */   public final List listPeopleVisible() throws BasicException {
//	com.openbravo.data.loader.StaticSentence@d4e78a
/* 155 */     return this.m_peoplevisible.list();
/*     */   }
/*     */   public final AppUser findPeopleByCard(String card) throws BasicException {
/* 158 */     return (AppUser)this.m_peoplebycard.find(card);
/*     */   }
/*     */ 
/*     */   public final String findRolePermissions(String sRole)
/*     */   {
/*     */     try {
/* 164 */       return Formats.BYTEA.formatValue(this.m_rolepermissions.find(sRole)); } catch (BasicException e) {
/*     */     }
/* 166 */     return null;
/*     */   }
/*     */ 
/*     */   public final void execChangePassword(Object[] userdata) throws BasicException
/*     */   {
/* 171 */     this.m_changepassword.exec(userdata);
/*     */   }
/*     */ 
/*     */   public final void resetResourcesCache() {
/* 175 */     this.resourcescache_Map = new HashMap();
/*     */   }
/*     */ 
/*     */   private final byte[] getResource(String name)
/*     */   {
/* 182 */     byte[] resource = (byte[])this.resourcescache_Map.get(name);
/*     */ 
/* 184 */     if (resource == null) {
/*     */       try
/*     */       {
/* 187 */         resource = (byte[])(byte[])this.m_resourcebytes.find(name);
/* 188 */         this.resourcescache_Map.put(name, resource);
/*     */       } catch (BasicException e) {
/* 190 */         resource = null;
/*     */       }
/*     */     }
/*     */ 
/* 194 */     return resource;
/*     */   }
/*     */ 
/*     */   public final void setResource(String name, int type, byte[] data)
/*     */   {
/* 199 */     Object[] value = { UUID.randomUUID().toString(), name, new Integer(type), data };
/*     */     try {
/* 201 */       if (this.m_resourcebytesupdate.exec(value) == 0) {
/* 202 */         this.m_resourcebytesinsert.exec(value);
/*     */       }
/* 204 */       this.resourcescache_Map.put(name, data);
/*     */     } catch (BasicException e) {
/*     */     }
/*     */   }
/*     */ 
/*     */   public final void setResourceAsBinary(String sName, byte[] data) {
/* 210 */     setResource(sName, 2, data);
/*     */   }
/*     */ 
/*     */   public final byte[] getResourceAsBinary(String sName) {
/* 214 */     return getResource(sName);
/*     */   }
/*     */ 
/*     */   public final String getResourceAsText(String sName) {
/* 218 */     return Formats.BYTEA.formatValue(getResource(sName));
/*     */   }
/*     */ 
/*     */   public final String getResourceAsXML(String sName) {
	//D:\workspace 空格\AtiBrow\print_templates\Ticket.Line.xml
	if(StartPOS.isFileMode)
		return filex.read(pathx.webAppPath()+"/print_templates/"+sName+".xml");
	else
/* 222 */     return Formats.BYTEA.formatValue(getResource(sName));
/*     */   }
/*     */ 
/*     */   public final BufferedImage getResourceAsImage(String sName) {
/*     */     try {
/* 227 */       byte[] img = getResource(sName);
/* 228 */       return img == null ? null : ImageIO.read(new ByteArrayInputStream(img)); } catch (IOException e) {
/*     */     }
/* 230 */     return null;
/*     */   }
/*     */ 
/*     */   public final void setResourceAsProperties(String sName, Properties p)
/*     */   {
/* 235 */     if (p == null)
/* 236 */       setResource(sName, 0, null);
/*     */     else
/*     */       try {
/* 239 */         ByteArrayOutputStream o = new ByteArrayOutputStream();
/* 240 */         p.storeToXML(o, "PFPOS", "UTF8");
/* 241 */         setResource(sName, 0, o.toByteArray());
/*     */       }
/*     */       catch (IOException e)
/*     */       {
/*     */       }
/*     */   }
/*     */ 
/*     */   public final Properties getResourceAsProperties(String sName) {
/* 249 */     Properties p = new Properties();
/*     */     try {
/* 251 */       byte[] img = getResourceAsBinary(sName);
/* 252 */       if (img != null)
/* 253 */         p.loadFromXML(new ByteArrayInputStream(img));
/*     */     }
/*     */     catch (IOException e) {
/*     */     }
/* 257 */     return p;
/*     */   }
/*     */ 
/*     */   public final int getSequenceCash(String host) throws BasicException {
/* 261 */     Integer i = (Integer)this.m_sequencecash.find(host);
/* 262 */     return i == null ? 1 : i.intValue();
/*     */   }
/*     */ 
/*     */   public final Object[] findActiveCash(String sActiveCashIndex) throws BasicException {
	//m activecash==com.openbravo.data.loader.StaticSentence@17d709f
/* 266 */     return (Object[])(Object[])this.m_activecash.find(sActiveCashIndex);
/*     */   }
/*     */ 
/*     */   public final void execInsertCash(Object[] cash) throws BasicException {
	//com.openbravo.data.loader.StaticSentence@142836a
/* 270 */     this.m_insertcash.exec(cash);
/*     */   }
/*     */ 
/*     */   public final String findLocationName(String iLocation) throws BasicException {
/* 274 */     return (String)this.m_locationfind.find(iLocation);
/*     */   }
/*     */ }

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.forms.DataLogicSystem
 * JD-Core Version:    0.6.0
 */